//
//  LeftViewController.swift
//  SwiftSideslipLikeQQ
//
//  Created by JohnLui on 15/4/11.
//  Copyright (c) 2015年 com.lvwenhan. All rights reserved.
//

import UIKit
import QorumLogs
import SlideMenuControllerSwift

public enum hiddenFlag:Int {
    case LEFT = 0
    case RIGHT = 1
}
// 侧滑菜单 View Controller
class userListController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    var quitImageView:UIImageView?
    var quitLabel:UILabel?
    var userNameLabel:UILabel?
    var functionList:UITableView?
    var functionListArr:NSArray = ["我的信息","评  分","开发者信息","建  议"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initLeftView()
        QL4("显示")
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }
    func initLeftView(){
        //创建背景
        let backIM = UIImageView(frame: CGRect(x: 0, y: 0, width: SlideMenuOptions.leftViewWidth, height: commonVariable.SYSTEM_HEIGHT))
        backIM.image = UIImage(named: "back")
        //创建头像
        let userHeader = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        switch((commonVariable.LOCAL_USER?.commonRoleType)!){
        case "0":
            userHeader.image = UIImage(named: "man")
            break
        case "1":
            userHeader.image = UIImage(named: "woman")
            break
        case "2":
            userHeader.image = UIImage(named: "baby")
            break
        default:
            userHeader.image = UIImage(named: "man")
            break
        }
        userHeader.center = CGPoint(x: 70, y: 90)
        //创建用户姓名标签
        self.userNameLabel = UILabel(frame: CGRect(x: 0, y: 0, width: commonVariable.SYSTEM_WIDTH/2, height: 50))
        self.userNameLabel?.center = CGPoint(x: commonVariable.SYSTEM_WIDTH/2, y: 90)
        self.userNameLabel?.font = UIFont.init(name: commonVariable.CHINAFONTNAME, size: 18)
        self.userNameLabel?.text = (commonVariable.LOCAL_USER!.userName == "") ? "夕阳下奔跑" : commonVariable.LOCAL_USER!.userName
        self.userNameLabel?.textAlignment = .Center
        //创建退出图片
        self.quitImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        self.quitImageView?.center = CGPoint(x: 50, y: commonVariable.SYSTEM_HEIGHT - 50)
        self.quitImageView?.image = UIImage(named: "exitIcon")
        //创建退出label
        self.quitLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 25))
        self.quitLabel?.text = "退 出"
        self.quitLabel?.textColor = commonVariable.DEEPREDCOLOR
        self.quitLabel?.font = UIFont.init(name: commonVariable.CHINAFONTNAME, size: 15)
        self.quitLabel?.center = CGPoint(x: 100, y: commonVariable.SYSTEM_HEIGHT - 50)
        self.quitLabel?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "userExit"))
        self.quitLabel?.userInteractionEnabled = true
        //功能列表
        self.functionList = UITableView(frame: CGRect(x: 20 , y: 150, width: commonVariable.SYSTEM_WIDTH/5*3, height: commonVariable.SYSTEM_HEIGHT - 150 - 80))
        self.functionList?.backgroundColor = UIColor.clearColor()
        functionList?.separatorStyle = .None
        functionList?.showsVerticalScrollIndicator = false
        functionList?.scrollEnabled = false
        functionList?.delegate = self
        functionList?.dataSource = self
        self.view.addSubview(backIM)
        self.view.addSubview(userHeader)
        self.view.addSubview(self.quitImageView!)
        self.view.addSubview(self.quitLabel!)
        self.view.addSubview(self.userNameLabel!)
        self.view.addSubview(self.functionList!)
    }
    
    
    func switchHidden(switchFlag:hiddenFlag){
        switch switchFlag {
        case .LEFT:
            self.quitLabel?.hidden = false
            self.quitImageView?.hidden = false
            self.userNameLabel!.hidden = false
            break
        case .RIGHT:
            self.quitLabel?.hidden = true
            self.quitImageView?.hidden = true
            self.userNameLabel!.hidden = true
            break
        default:
            break
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //功能列表代理
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let tempCell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "function_\(indexPath.row)")
        tempCell.detailTextLabel?.text = functionListArr[indexPath.row] as! String
        tempCell.detailTextLabel?.textColor = UIColor.whiteColor()
        tempCell.backgroundColor = UIColor.clearColor()
        tempCell.detailTextLabel?.font = UIFont(name: commonVariable.CHINAFONTNAME, size: 15)
        return tempCell
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        switch(indexPath.row){
        case 0:
            //初始化，防止信息滞留
            commonVariable.HOSTCLASS?.closeLeft()
            commonVariable.USERINFOCLASS = meInfoController()
            commonVariable.USERINFOCLASS!.modalTransitionStyle = .CrossDissolve
            commonVariable.RECORDERCONTROLLER!.presentViewController(commonVariable.USERINFOCLASS!, animated: true, completion: { () -> Void in
                
            })
            break
        case 1:
            commonVariable.HOSTCLASS?.closeLeft()
            let urlString = "itms-apps://itunes.apple.com/app/id1258412949"
            let url = NSURL(string: urlString)
            UIApplication.sharedApplication().openURL(url!)
            break
        case 2:
            if(commonVariable.DEVELOPINFOCLASS == nil){
                commonVariable.DEVELOPINFOCLASS = setDetailShowViewControl()
            }
            let headerImage = UIImageView(frame: CGRectMake(0, 0, 70, 70))
            headerImage.image = UIImage(named: "developHeader")
            commonVariable.DEVELOPINFOCLASS!.headerImage = headerImage
            let mainContentView = UIView()
            mainContentView.frame = CGRectMake(0, 185, commonVariable.SYSTEM_WIDTH, 800)
            let textView = UITextView(frame: CGRectMake(0,0,commonVariable.SYSTEM_WIDTH - 40,800))
            textView.editable = false
            textView.selectable = false
            textView.backgroundColor = commonVariable.BACKGROUNDCOLOR
            textView.font = UIFont(name: "Helvetica-Bold", size: 15)
            textView.textColor = commonVariable.IRONCOLOR
            textView.scrollEnabled = false
            //通过富文本来设置行间距
            let paraph = NSMutableParagraphStyle()
            //将行间距设置为28
            paraph.lineSpacing = 20
            //样式属性集合
            let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(15),NSParagraphStyleAttributeName: paraph,NSForegroundColorAttributeName:commonVariable.IRONCOLOR]
            let text = "本软件，率性而作，在此感谢:\n    · 一直对我无解支持的老婆\n   · 一直伴我左右的赵大炮(我兄弟)\n   · 促使我灵感迸发并喜欢撞桌腿的王铁头(我儿子)\n          愿时间常驻，你我童心仍未泯"
            textView.attributedText = NSAttributedString(string: text, attributes: attributes)
            textView.textAlignment = NSTextAlignment.Left
            mainContentView.addSubview(textView)
            commonVariable.DEVELOPINFOCLASS!.contentView = mainContentView
            commonVariable.DEVELOPINFOCLASS!.contentViewHeight = 800
            commonVariable.DEVELOPINFOCLASS!.contentViewWeight = commonVariable.SYSTEM_WIDTH - 40
            commonVariable.DEVELOPINFOCLASS!.reloadInputViews()
            self.presentViewController(commonVariable.DEVELOPINFOCLASS!, animated: true) { () -> Void in
            }
            commonVariable.HOSTCLASS?.closeLeft()
            break
        case 3:
            commonVariable.HOSTCLASS?.closeLeft()
            if(commonVariable.USERSUGGESTION == nil){
                commonVariable.USERSUGGESTION = suggestViewController()
            }
            self.presentViewController(commonVariable.USERSUGGESTION!, animated: true, completion: nil)
        default:
            break
        }
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return functionListArr.count
    }
    //用户退出按钮响应事件
    func userExit(){
        commonVariable.HOSTCLASS!.closeLeft()
        if(commonVariable.LOGINVIEW == nil){
            commonVariable.LOGINVIEW = UIStoryboard(name: "LoginView", bundle: nil).instantiateInitialViewController() as! loginViewController
            commonVariable.HOSTCLASS?.presentViewController(commonVariable.LOGINVIEW!, animated: true, completion: { () -> Void in
                
            })
        }else{
            commonVariable.HOSTCLASS?.dismissViewControllerAnimated(true, completion: { () -> Void in
            })
        }
    }
    func leftAction() {
        switchHidden(hiddenFlag.LEFT)
    }
    func rightAction() {
        switchHidden(hiddenFlag.RIGHT)
    }
    
}
